var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'employee_search_ajs.html', controller: ListEmpMstCtrl}).
      when('/deleteemp/', {templateUrl: 'employee_search_ajs.html', controller: ListEmpMstCtrl}).
     //  when('/editcust/:custId', {templateUrl: 'employee_search_ajs.html', controller: ListEmpMstCtrl}). 
	  when('/editemp/', {templateUrl: 'employee_search_ajs.html', controller: ListEmpMstCtrl}). 
      when('/updtemp/', {templateUrl: 'employee_search_ajs.html', controller: ListEmpMstCtrl}). 
      when('/cancel/', {templateUrl: 'employee_search_ajs.html', controller: ListEmpMstCtrl}). 
      otherwise({redirectTo: '/'});
}]);


app.service('ContactService', function () {
   //  $window.alert("Hello Details  "+empid);
    this.get = function (empid,$window) {
        for (i in employeemast) {
            if (employeemast[i].empid == empid) {
                 $window.alert("Edit Customer Details  "+empid);
                return employeemast[i];
            }
        }
 
    };
}
);


function ListEmpMstCtrl($scope, $http,$window,ContactService,$location)
 {
    $scope.currentPage = 0;
    $scope.pageSize = 15;
	$scope.prefx = [{ id: 1, name: 'Mr'}, { id: 2, name: 'Mrs'}];
    $scope.designation = [{ id: 1, name: 'EMPLOYEE'}, { id: 2, name: 'TRAINEE'}];
	
	$scope.newemployeemast = {};
	$scope.Address = "";
	$scope.email = "";
	$scope.city = "";
	$scope.phoneno = "";
	$scope.Math = window.Math;
	
    $http.get('employeedata.php').success(function(data) {
    $scope.employeemast = data;
    });
    
     
    
    $scope.edit = function(empid,city,email,phoneno,Address) {
     // $window.alert("Edit Customer Details  "+custid);
       // var i =0;
          for(i in $scope.employeemast) {
            if($scope.employeemast[i].empid == empid)
            {
             //   $window.alert(" value of empid is "+i+ "  "+empid+" "+city+" "+email+" "+phoneno+" "+Address);
               //we use angular.copy() method to create copy of original object
               $scope.newemployeemast = angular.copy($scope.employeemast[i]);
			   $scope.city = city;
			   $scope.email = email;
			   $scope.phoneno = phoneno; 
			   $scope.Address = Address;
            }
        }
          };
    
      
   $scope.deleteemp = function(item) {
      //$window.alert("Deletion Flow"+item);
        if (confirm('Do you really want to delete this Employee record?')) 
        {
         //   $window.alert(item+' Record is being Deleted');
            $http.post('employee_search_deletedata.php',{'empid': item}).success(function(data)
            {
                $window.alert( 'Record for ' + item+ 'got deleted Successfully');
                 $scope.activePath = $location.path('employee_search_ajs.php#/editemp/');
                 window.location.href = '#/editemp/';
            });
             
        }
      };
      
	  	//Function to Update Data in the Employee Master  
	  $scope.updtempdata = function(newemployeemast,city,email,phoneno,Address) 
		{   
			$window.alert("Updation of Record for "+newemployeemast.prefx+","+newemployeemast.empname+","+Address+","+city+","+newemployeemast.designation+","+email+","+phoneno+","+newemployeemast.sal);
			var sql = 'employee_search_updatedata.php?'+'&empname='+newemployeemast.empname+'&prefx='+newemployeemast.prefx +'&address='+Address+'&city='+city+'&email='+email+'&phoneno='+phoneno+'&empid='+newemployeemast.empid+'&designation='+newemployeemast.designation+'&sal='+newemployeemast.sal;
			//$window.alert(sql);
			$http.post(sql).success(function(data)  
			{
			$window.alert( 'Record for '+ newemployeemast.prefx  + newemployeemast.empname + 'got Successfully Updated');
			window.location.href = 'employee_search_ajs.php#/editemp/';
			});
		};
      
	  
	  
	  
	 //Function to Save Data in the Employee Master 
	   $scope.saveempdata = function(newemployeemast,Address,city,email,phoneno) 
	  	{   
			$window.alert(" value to insert are "+newemployeemast.designation+" "+newemployeemast.prefx+" "+newemployeemast.empname+" " +Address+" "+city+" "+email+" "+phoneno+" "+newemployeemast.sal);
			var sql = 'employee_search_insertdata.php?'+'&prefx='+newemployeemast.prefx+'&empid='+newemployeemast.empid+'&empname='+newemployeemast.empname+'&Address='+Address+'&city='+city+'&email='+email+'&phoneno='+phoneno+'&sal='+newemployeemast.sal+'&designation='+newemployeemast.designation;
			//$window.alert(sql);
			
			$http.post(sql).success(function(data)  
			{
				$window.alert( 'Record for '+ newemployeemast.empname + 'got Successfully Saved');
				window.location.href = 'employee_search_ajs.php#/editemp/';
			});  
		};  
	  
	   //Function to Clear the selected parameters in the search screen.
        $scope.cancel= function()
		{
			$scope.activePath = null;
			$scope.activePath = $location.path('employee_search_ajs.php#/editemp/');
			window.location.href = '#/editemp/';
		};
      
 }
    
    //Function to convert the entered value in UI into uppercase
     app.directive('capitalize', function () {
        return {
            require: 'ngModel',
            link: function (scope, element, attrs, modelCtrl) {
                var capitalize = function (inputValue) {
                    if (angular.isUndefined(inputValue))
                        return;
 
                    var capitalized = inputValue.toUpperCase();
                    if (capitalized !== inputValue) {
                        modelCtrl.$setViewValue(capitalized);
                        modelCtrl.$render();
                    }
                    return capitalized;
                };
                modelCtrl.$parsers.push(capitalize);
                capitalize(scope[attrs.ngModel]);  // capitalize initial value
            }
        };
    });
    
    //Custom Filter Function to sort the record.
    app.filter('startingFrom', function() 
    {
            return function(input, start) 
            {
                start = +start; //parse to int
                return input.slice(start);
            };
    });
        
        /* 
 function AddCustCtrl($scope, $http, $location,$window) 
   {
      $scope.master = {};
      $scope.activePath = null;
      $scope.add_new = function(cust,AddNewForm) 
	  {
       console.log(cust);
		$window.alert(" value to insert are empid is "+prefx+ "  "+empid+" "+empname+" " +Address+" "+city+" "+email+" "+phoneno+" "+sal);
             
		$http.post('saveemployee_ajs.php', cust).success(function()
			{
			$window.alert( 'Record got Successfully Added');        
            $scope.reset();
            $scope.activePath = $location.path('employee_search_ajs.php#/editemp/');
            window.location.href = '#/editemp/';
           });
             
        $scope.reset = function() 
        {
           $scope.newemployeemast = angular.copy($scope.master);
        };
			$scope.reset();
      };
  
      $scope.prefx = [{ id: 1, name: 'Mr'}, { id: 2, name: 'Mrs'}];
      $scope.designation = [{ id: 1, name: 'EMPLOYEE'}, { id: 2, name: 'TRAINEE'}];
    }


ctrl.directive('handleDestroy', function() {
    return function(scope, tElement, attributes) {        
        scope.$on('$destroy', function() {
            alert("In destroy of:" + scope.todo.text);
        });
    };
});


 */